\subsection{hal-\/emu.c File Reference}
\label{hal-emu_8c}\index{hal-\/emu.c@{hal-\/emu.c}}


HAL emulation for Linux.  


{\ttfamily \#include $<$config.h$>$}\par
{\ttfamily \#include $<$sio/sio.h$>$}\par
{\ttfamily \#include $<$stdio.h$>$}\par
{\ttfamily \#include $<$stdlib.h$>$}\par
{\ttfamily \#include $<$string.h$>$}\par
{\ttfamily \#include $<$crypt.h$>$}\par
{\ttfamily \#include $<$sw.h$>$}\par
{\ttfamily \#include $<$types.h$>$}\par
{\ttfamily \#include $<$sys/types.h$>$}\par
{\ttfamily \#include $<$sys/stat.h$>$}\par
{\ttfamily \#include $<$fcntl.h$>$}\par
{\ttfamily \#include $<$unistd.h$>$}\par
{\ttfamily \#include $<$errno.h$>$}\par
\subsubsection*{Defines}
\begin{DoxyCompactItemize}
\item 
\#define {\bf PORT}~\char`\"{}/dev/ttyS0\char`\"{}
\end{DoxyCompactItemize}
\subsubsection*{Functions}
\begin{DoxyCompactItemize}
\item 
void {\bf hal\_\-init} (void)
\begin{DoxyCompactList}\small\item\em Initializes the HAL. \item\end{DoxyCompactList}\item 
void {\bf hal\_\-destroy} (void)
\item 
{\bf bool} {\bf hal\_\-eeprom\_\-read} ({\bf iu8} $\ast${\bf dst}, {\bf iu16} src, {\bf iu8} len)
\begin{DoxyCompactList}\small\item\em Read data from EEPROM. \item\end{DoxyCompactList}\item 
{\bf bool} {\bf hal\_\-eeprom\_\-write} ({\bf iu16} {\bf dst}, {\bf iu8} $\ast$src, {\bf iu8} len)
\begin{DoxyCompactList}\small\item\em Write data to EEPROM. \item\end{DoxyCompactList}\item 
void {\bf hal\_\-io\_\-sendByteT0} ({\bf iu8} b)
\begin{DoxyCompactList}\small\item\em Send a byte with T=0 error detection and recovery. \item\end{DoxyCompactList}\item 
{\bf iu8} {\bf hal\_\-io\_\-recByteT0} (void)
\begin{DoxyCompactList}\small\item\em Receive a byte with T=0 error detection and recovery. \item\end{DoxyCompactList}\item 
{\bf bool} {\bf hal\_\-rnd\_\-getBlock} ({\bf iu8} $\ast${\bf dst})
\begin{DoxyCompactList}\small\item\em Returns 8 random bytes. \item\end{DoxyCompactList}\end{DoxyCompactItemize}
\subsubsection*{Variables}
\begin{DoxyCompactItemize}
\item 
{\bf iu8} {\bf eeprom} [1 $\ast$1024]
\item 
int {\bf halsend}
\end{DoxyCompactItemize}


\subsubsection{Detailed Description}
HAL emulation for Linux. \begin{DoxyParagraph}{Id:}
\doxyref{hal-\/emu.c}{p.}{hal-emu_8c},v 1.18 2003/03/30 12:42:21 m Exp 
\end{DoxyParagraph}


\subsubsection{Define Documentation}
\index{hal-\/emu.c@{hal-\/emu.c}!PORT@{PORT}}
\index{PORT@{PORT}!hal-emu.c@{hal-\/emu.c}}
\paragraph[{PORT}]{\setlength{\rightskip}{0pt plus 5cm}\#define PORT~\char`\"{}/dev/ttyS0\char`\"{}}\hfill\label{hal-emu_8c_a614217d263be1fb1a5f76e2ff7be19a2}


Referenced by hal\_\-init().



\subsubsection{Function Documentation}
\index{hal-\/emu.c@{hal-\/emu.c}!hal\_\-destroy@{hal\_\-destroy}}
\index{hal\_\-destroy@{hal\_\-destroy}!hal-emu.c@{hal-\/emu.c}}
\paragraph[{hal\_\-destroy}]{\setlength{\rightskip}{0pt plus 5cm}void hal\_\-destroy (
\begin{DoxyParamCaption}
\item[{void}]{}
\end{DoxyParamCaption}
)}\hfill\label{hal-emu_8c_af989aaa3712fd474689c704133f0692b}
\index{hal-\/emu.c@{hal-\/emu.c}!hal\_\-eeprom\_\-read@{hal\_\-eeprom\_\-read}}
\index{hal\_\-eeprom\_\-read@{hal\_\-eeprom\_\-read}!hal-emu.c@{hal-\/emu.c}}
\paragraph[{hal\_\-eeprom\_\-read}]{\setlength{\rightskip}{0pt plus 5cm}{\bf bool} hal\_\-eeprom\_\-read (
\begin{DoxyParamCaption}
\item[{{\bf iu8} $\ast$}]{ dst, }
\item[{{\bf iu16}}]{ src, }
\item[{{\bf iu8}}]{ len}
\end{DoxyParamCaption}
)}\hfill\label{hal-emu_8c_ae23736f92fbd6e59f6e67a191e69ecef}


Read data from EEPROM. 

The internal EEPROM begins at address 0, the external EEPROM is located subsequently.


\begin{DoxyParams}{Parameters}
\item[{\em dst}]Pointer to destination area. \item[{\em src}]EEPROM source address. \item[{\em len}]Length of data in bytes.\end{DoxyParams}

\begin{DoxyRetVals}{Return values}
\item[{\em TRUE}]on success. \item[{\em FALSE}]on failure. Error code given in sw. \end{DoxyRetVals}


Referenced by auth\_\-createVerifyCryptogram(), auth\_\-verifyPin(), cmd\_\-read(), fs\_\-init(), fstream\_\-read(), hal\_\-rnd\_\-getBlock(), main(), and ta\_\-commit().

\index{hal-\/emu.c@{hal-\/emu.c}!hal\_\-eeprom\_\-write@{hal\_\-eeprom\_\-write}}
\index{hal\_\-eeprom\_\-write@{hal\_\-eeprom\_\-write}!hal-emu.c@{hal-\/emu.c}}
\paragraph[{hal\_\-eeprom\_\-write}]{\setlength{\rightskip}{0pt plus 5cm}{\bf bool} hal\_\-eeprom\_\-write (
\begin{DoxyParamCaption}
\item[{{\bf iu16}}]{ dst, }
\item[{{\bf iu8} $\ast$}]{ src, }
\item[{{\bf iu8}}]{ len}
\end{DoxyParamCaption}
)}\hfill\label{hal-emu_8c_a19f6e1f1e627521793f250459c5e49b8}


Write data to EEPROM. 

The internal EEPROM begins at address 0, the external EEPROM is located subsequently.


\begin{DoxyParams}{Parameters}
\item[{\em dst}]EEPROM destination address \item[{\em src}]Pointer to source area. \item[{\em len}]Length of data in bytes.\end{DoxyParams}

\begin{DoxyRetVals}{Return values}
\item[{\em TRUE}]on success. \item[{\em FALSE}]on failure. Error code given in sw. \end{DoxyRetVals}


Referenced by auth\_\-createVerifyCryptogram(), auth\_\-setPin(), auth\_\-verifyPin(), cmd\_\-write(), fstream\_\-write(), hal\_\-rnd\_\-getBlock(), log\_\-add(), log\_\-init(), ta\_\-commit(), and ta\_\-setdata().

\index{hal-\/emu.c@{hal-\/emu.c}!hal\_\-init@{hal\_\-init}}
\index{hal\_\-init@{hal\_\-init}!hal-emu.c@{hal-\/emu.c}}
\paragraph[{hal\_\-init}]{\setlength{\rightskip}{0pt plus 5cm}void hal\_\-init (
\begin{DoxyParamCaption}
\item[{void}]{}
\end{DoxyParamCaption}
)}\hfill\label{hal-emu_8c_af5ea8985a12feedc3ac25fbb50712183}


Initializes the HAL. 

Must be called after each reset. 

Referenced by main().

\index{hal-\/emu.c@{hal-\/emu.c}!hal\_\-io\_\-recByteT0@{hal\_\-io\_\-recByteT0}}
\index{hal\_\-io\_\-recByteT0@{hal\_\-io\_\-recByteT0}!hal-emu.c@{hal-\/emu.c}}
\paragraph[{hal\_\-io\_\-recByteT0}]{\setlength{\rightskip}{0pt plus 5cm}{\bf iu8} hal\_\-io\_\-recByteT0 (
\begin{DoxyParamCaption}
\item[{void}]{}
\end{DoxyParamCaption}
)}\hfill\label{hal-emu_8c_a6f2a2f946cf90cb7bd613f2c3ada355c}


Receive a byte with T=0 error detection and recovery. 

Currently only 9600 bps at 3.58MHz with direct convention is supported.

\begin{DoxyReturn}{Returns}
This function returns a received byte. There can't be an error, because T=0 tries endlessly to receive a characters. (And if there is really an error, what should SOSSE do with it anyway?) 
\end{DoxyReturn}


Referenced by cmd\_\-delete(), cmd\_\-select(), cmd\_\-updateBinary(), cmd\_\-write(), main(), and t0\_\-recBlock().

\index{hal-\/emu.c@{hal-\/emu.c}!hal\_\-io\_\-sendByteT0@{hal\_\-io\_\-sendByteT0}}
\index{hal\_\-io\_\-sendByteT0@{hal\_\-io\_\-sendByteT0}!hal-emu.c@{hal-\/emu.c}}
\paragraph[{hal\_\-io\_\-sendByteT0}]{\setlength{\rightskip}{0pt plus 5cm}void hal\_\-io\_\-sendByteT0 (
\begin{DoxyParamCaption}
\item[{{\bf iu8}}]{ b}
\end{DoxyParamCaption}
)}\hfill\label{hal-emu_8c_a50f4270db8dcf34e7e98f1e2b43d1135}


Send a byte with T=0 error detection and recovery. 

Currently only 9600 bps at 3.58MHz with direct convention is supported.


\begin{DoxyParams}{Parameters}
\item[{\em b}]Byte to send. \end{DoxyParams}


Referenced by cmd\_\-getChallenge(), cmd\_\-getResponse(), cmd\_\-read(), cmd\_\-readBinary(), main(), t0\_\-sendAck(), t0\_\-sendCAck(), t0\_\-sendSw(), and t0\_\-sendWord().

\index{hal-\/emu.c@{hal-\/emu.c}!hal\_\-rnd\_\-getBlock@{hal\_\-rnd\_\-getBlock}}
\index{hal\_\-rnd\_\-getBlock@{hal\_\-rnd\_\-getBlock}!hal-emu.c@{hal-\/emu.c}}
\paragraph[{hal\_\-rnd\_\-getBlock}]{\setlength{\rightskip}{0pt plus 5cm}{\bf bool} hal\_\-rnd\_\-getBlock (
\begin{DoxyParamCaption}
\item[{{\bf iu8} $\ast$}]{ dst}
\end{DoxyParamCaption}
)}\hfill\label{hal-emu_8c_a4dfdfc55f93a2f674edd64b10e8332ae}


Returns 8 random bytes. 

{\itshape The current AVR implementation does not produce real random numbers, but pseudo random numbers. These are produced by a X9.17 generator modified to use a counter instead of a timer. The state of this PRNG is held at RAND\_\-STATE\_\-ADDR. The start value (at least the key) at this address should be unique for every card.\/}


\begin{DoxyParams}{Parameters}
\item[{\em dst}]Pointer to the destination, where the 8 random bytes should be written.\end{DoxyParams}

\begin{DoxyRetVals}{Return values}
\item[{\em TRUE}]on success. \item[{\em FALSE}]on failure. Error code given in sw.\end{DoxyRetVals}
\begin{DoxyNote}{Note}
There is still a simpler PRNG in the code, which uses 16 bytes RAM and 126 bytes ROM less than the X9.17 generator. 
\end{DoxyNote}


Referenced by auth\_\-getChallenge(), and main().



\subsubsection{Variable Documentation}
\index{hal-\/emu.c@{hal-\/emu.c}!eeprom@{eeprom}}
\index{eeprom@{eeprom}!hal-emu.c@{hal-\/emu.c}}
\paragraph[{eeprom}]{\setlength{\rightskip}{0pt plus 5cm}{\bf iu8} {\bf eeprom}[1 $\ast$1024]}\hfill\label{hal-emu_8c_af9f13c6e307e56818b6c7b54da194fdf}


Referenced by hal\_\-eeprom\_\-read(), hal\_\-eeprom\_\-write(), hal\_\-init(), and hal\_\-io\_\-recByteT0().

\index{hal-\/emu.c@{hal-\/emu.c}!halsend@{halsend}}
\index{halsend@{halsend}!hal-emu.c@{hal-\/emu.c}}
\paragraph[{halsend}]{\setlength{\rightskip}{0pt plus 5cm}int {\bf halsend}}\hfill\label{hal-emu_8c_a0daf42abcdf1a78febf9353bfdc2f773}


Referenced by hal\_\-init(), hal\_\-io\_\-recByteT0(), and hal\_\-io\_\-sendByteT0().

